#!/usr/bin/env bash
set -e

BUILD_DIR=build
TOOLCHAIN=toolchain/arm64-toolchain.cmake
OPT_LEVELS=("O0" "O2" "O3")
ARCHS=("native" "arm64")

for arch in "${ARCHS[@]}"; do
  for opt in "${OPT_LEVELS[@]}"; do
    outdir="${BUILD_DIR}/${arch}/${opt}"
    mkdir -p "$outdir"
    pushd "$outdir" >/dev/null

    cmake ../../.. \
      -DCMAKE_BUILD_TYPE=Release \
      -DCMAKE_C_FLAGS="-${opt}" \
      -DCMAKE_CXX_FLAGS="-${opt}" \
      $([ "$arch" == "arm64" ] && echo "-DCMAKE_TOOLCHAIN_FILE=../../../${TOOLCHAIN}")

    make clean
    make -j

    # 验证架构
    file app

    popd >/dev/null
  done
done
